package org.ehe.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.github.linpeilie.Converter;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.ehe.common.mybatis.core.page.PageQuery;
import org.ehe.common.mybatis.core.page.TableDataInfo;
import org.ehe.system.domain.HrEmployeeAttachment;
import org.ehe.system.domain.bo.HrEmployeeAttachmentBo;
import org.ehe.system.domain.vo.HrEmployeeAttachmentVo;
import org.ehe.system.mapper.HrEmployeeAttachmentMapper;
import org.ehe.system.service.IHrEmployeeAttachmentService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 员工附件Service业务层处理
 *
 * @author zhangnn
 */
@RequiredArgsConstructor
@Service
public class HrEmployeeAttachmentServiceImpl implements IHrEmployeeAttachmentService {

    private final HrEmployeeAttachmentMapper baseMapper;
    private final Converter converter;

    /**
     * 查询员工附件
     */
    @Override
    public HrEmployeeAttachmentVo queryById(Long attachmentId) {
        return baseMapper.selectVoById(attachmentId);
    }

    /**
     * 根据工号查询员工附件列表
     */
    @Override
    public List<HrEmployeeAttachmentVo> queryListByEmployeeNo(String employeeNo) {
        return baseMapper.selectVoList(new LambdaQueryWrapper<HrEmployeeAttachment>()
                .eq(HrEmployeeAttachment::getEmployeeNo, employeeNo)
                .orderByDesc(HrEmployeeAttachment::getCreateTime));
    }

    /**
     * 查询员工附件列表
     */
    @Override
    public TableDataInfo<HrEmployeeAttachmentVo> queryPageList(HrEmployeeAttachmentBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<HrEmployeeAttachment> lqw = buildQueryWrapper(bo);
        Page<HrEmployeeAttachmentVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询员工附件列表
     */
    @Override
    public List<HrEmployeeAttachmentVo> queryList(HrEmployeeAttachmentBo bo) {
        LambdaQueryWrapper<HrEmployeeAttachment> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<HrEmployeeAttachment> buildQueryWrapper(HrEmployeeAttachmentBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HrEmployeeAttachment> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getEmployeeNo()), HrEmployeeAttachment::getEmployeeNo, bo.getEmployeeNo());
        lqw.eq(StringUtils.isNotBlank(bo.getAttachmentType()), HrEmployeeAttachment::getAttachmentType,
                bo.getAttachmentType());
        lqw.like(StringUtils.isNotBlank(bo.getAttachmentName()), HrEmployeeAttachment::getAttachmentName,
                bo.getAttachmentName());
        lqw.between(params.get("beginUploadTime") != null && params.get("endUploadTime") != null,
                HrEmployeeAttachment::getUploadTime, params.get("beginUploadTime"), params.get("endUploadTime"));
        lqw.orderByDesc(HrEmployeeAttachment::getCreateTime);
        return lqw;
    }

    /**
     * 新增员工附件
     */
    @Override
    public Boolean insertByBo(HrEmployeeAttachmentBo bo) {
        HrEmployeeAttachment add = converter.convert(bo, HrEmployeeAttachment.class);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setAttachmentId(add.getAttachmentId());
        }
        return flag;
    }

    /**
     * 修改员工附件
     */
    @Override
    public Boolean updateByBo(HrEmployeeAttachmentBo bo) {
        HrEmployeeAttachment update = converter.convert(bo, HrEmployeeAttachment.class);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 批量删除员工附件
     */
    @Override
    public Boolean deleteByIds(Collection<Long> attachmentIds) {
        return baseMapper.deleteByIds(attachmentIds) > 0;
    }

}
